<template>
  <div class='container'>
    <!-- 卡片区域 -->
    <el-card>
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id!==undefined">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{routeSubjectName}}</el-breadcrumb-item>
        <el-breadcrumb-item>标签管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" :model="queryInfo" ref="tagsFormRef">
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="queryInfo.tagName" size="small"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select v-model="queryInfo.state" placeholder="请选择" size="small">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clearTags">清空</el-button>
              <el-button type="primary" size="small" @click="getTagList">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button style="float: right; padding: 10px" type="success" icon="el-icon-edit" size="small"
            @click="addTags">新增标签
          </el-button>
          <el-button type="text" style="float: right; padding: 10px" v-if="$route.query.id!==undefined"
            @click="skipSubject" icon="el-icon-back">返回学科</el-button>
        </el-col>
      </el-row>
      <!-- 消息提示区域 -->
      <el-alert type="info" show-icon :closable="false">
        <div slot="title">数据一共 {{ totals }} 条</div>
      </el-alert>
      <!-- 数据操作 -->
      <el-table :data="tagsList" stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="80px"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <div>{{ scope.row.state === 1 ? "已启用" : "已禁用" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="changeState(scope.row)">
              <div>{{ scope.row.state === 0 ? "启用" : "禁用" }}</div>
            </el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="editTag(scope.row)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="deleteTag(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background class="page" @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.page" :page-sizes="[2, 5, 10]" :page-size="queryInfo.pagesize"
        layout="prev, pager, next, sizes, jumper" :total="totals" style="margin-top: 20px; text-align: right;">
      </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="新增目录" :visible.sync="dialogTagAdd" width="400px" @close='handleClose'>
      <el-form :model="addTagForm" :rules="addTagFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="所属学科" size="small" prop="subjectID" v-if="!routeId">
          <el-select v-model="addTagForm.subjectID" placeholder="请选择所属学科">
            <!-- 遍历目录列表渲染下拉菜单 -->
            <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" size="small" prop="tagName">
          <el-input v-model="addTagForm.tagName" placeholder="请输入目录名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogTagAdd = false">取 消</el-button>
        <el-button type="primary" @click="submitAddTag">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /添加对话框 -->
    <!-- 修改对话框  -->
    <el-dialog title="修改目录" :visible.sync="dialogEditTag" width="400px" @close="changeClose">
      <el-form :model="editTagForm" :rules="editTagFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="所属学科" size="small" prop="subjectID">
          <el-select v-model="editTagForm.subjectID">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" size="small" prop="tagName">
          <el-input v-model="editTagForm.tagName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEditTag = false">取 消</el-button>
        <el-button type="primary" @click="dialogEditTagForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- / 修改对话框  -->
  </div>
</template>

<script>
import { list, add, changeState, remove, update } from '@/api/hmmm/tags.js'
import { list as subjects } from '@/api/hmmm/subjects.js'
// import tagsAddVue from '../components/tags-add.vue'
export default {
  data () {
    return {
      // 接收的id和名称
      routeSubjectName: '',
      routeId: null,
      // 获取学科列表数据
      subjectList: [],
      // 获取标签的列表
      tagsList: [],
      // 获取查询用户信息的参数
      queryInfo: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        tagName: null,
        state: null
      },
      // 总数据条数
      totals: 0,
      // 添加对话框的显示与隐藏
      dialogTagAdd: false,
      // 修改对话框的显示与隐藏
      dialogEditTag: false,
      // 添加表单数据对象
      addTagForm: {
        tagName: '',
        subjectID: ''
      },
      // 验证添加表单的规则
      addTagFormRules: {
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
          // { min: 1, max: 9, message: '长度在 1 到 9 个字符', trigger: 'blur' }
        ]
      },
      // 修改表单的验证规则
      editTagFormRules: {
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
          // { min: 1, max: 9, message: '长度在 1 到 9 个字符', trigger: 'blur' }
        ]
      },
      // 修改表单数据对象
      editTagForm: {
        subjectID: '',
        tagName: '',
        // 修改的数据id
        id: ''
      }
    }
  },
  methods: {
    // 点击跳转到学科页面
    skipSubject () {
      this.$router.push({ path: '/subjects/list' })
    },
    // 点击修改弹框的确定按钮修改数据
    async dialogEditTagForm () {
      const res = await update(this.editTagForm)
      // console.log(res)
      this.dialogEditTag = false
      if (res.status !== 200) return this.$message.error('失败')
      this.$message.success('修改标签成功')
      this.getTagList()
    },
    // 点击修改更能 获取需要的数据和id
    editTag (row) {
      this.dialogEditTag = true
      console.log(row)
      this.editTagForm.subjectID = row.subjectID
      this.editTagForm.tagName = row.tagName
      this.editTagForm.id = row.id
    },
    // 点击删除功能
    deleteTag (id) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id })
        // console.log(res)
        if (res.status !== 200) return this.$message.error('删除失败')
        this.getTagList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击切换禁用状态
    async changeState (row) {
      // console.log(row)
      const res = await changeState({ id: row.id, state: row.state === 0 ? 1 : 0 })
      // console.log(res)
      if (res.status !== 200) return this.$message.error('失败')
      row.state = row.state === 0 ? 1 : 0
      this.$message.success('操作成功')
    },
    // 点击确定添加标签按钮
    async submitAddTag () {
      const res = await add(this.addTagForm)
      this.dialogTagAdd = false
      // console.log(res)
      if (res.status !== 200) return this.$message.error('失败')
      this.$message.success('新增标签成功')
      this.getTagList()
      console.log(res)
    },
    // 关闭修改弹框自定义校验
    changeClose () {
      this.$refs.editFormRef.resetFields()
    },
    // 关闭弹框自定义校验
    handleClose () {
      this.$refs.addFormRef.resetFields()
      this.addTagForm.subjectID = ''
    },
    // 点击弹出新增标签
    addTags () {
      this.dialogTagAdd = true
    },
    // 点击清除功能
    clearTags () {
      this.$refs.tagsFormRef.resetFields()
      this.getTagList()
    },
    // 更新当前页数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getTagList()
    },
    // 更新当前页面
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getTagList()
    },
    // 获取标签数据
    async getTagList () {
      const { data: res } = await list(this.queryInfo)
      // console.log(res)
      this.tagsList = res.items
      this.totals = res.counts
    },
    // 获取学科数据
    async getSubjectList () {
      // console.log(123)
      const { data: res } = await subjects()
      // console.log(res)
      this.subjectList = res.items
    }
  },
  created () {
    this.getTagList()
    this.getSubjectList()
    this.routeId = this.$route.query.id
    this.routeSubjectName = this.$route.query.subjectName
    // this.addTagForm.subjectID = this.$route.query.id - 0
    if (this.$route.query.id !== undefined) {
      this.addTagForm.subjectID = this.$route.query.id - 0
    }
    this.queryInfo.subjectID = (this.$route.query.id && +this.$route.query.id) || null
  }
}
</script>

<style scoped lang='less'>
.el-breadcrumb {
  margin-bottom: 20px;
  padding: 0 0 15px;
  border-bottom: 1px solid #dcdfe6;
}
.el-card {
  margin: 10px;
}
.el-alert {
  margin-bottom: 15px;
}
</style>
